/**
 * 
 */
package com.ermax.aircond.common.service.impl;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ermax.aircond.common.dao.WriteMachineDataDao;
import com.ermax.aircond.common.dao.impl.WriteMachineDataTimerTask;
import com.ermax.aircond.common.domain.WriteMachineData;
import com.ermax.aircond.common.service.WriteMachineDataService;

/**
 * @author Eric
 *
 */
@Service("writeMachineDataService")
public class WriteMachineDataServiceImpl implements WriteMachineDataService {

	private static final Logger logger = Logger.getLogger(WriteMachineDataServiceImpl.class);
	
	@Autowired
	private WriteMachineDataDao writeMachineDataDao;
	
	/* (non-Javadoc)
	 * @see com.ermax.aircond.common.service.WriteMachineDataService#insert(com.ermax.aircond.common.domain.WriteMachineData)
	 */
	@Override
	@Transactional
	public int insert(WriteMachineData writeMachineData) {
		// TODO Auto-generated method stub
		logger.debug("Insert!");
		writeMachineDataDao.insert(writeMachineData);
		
		return 0;
	}

	@Override
	@Transactional(readOnly = true)
	public WriteMachineData select(short nid) {
		return writeMachineDataDao.select(nid);
	}

	@Override
	public void insertUpdate(WriteMachineData writeMachineData) {
		logger.debug("Insert Update!");
		
		//WriteMachineData updateData = writeMachineDataDao.select(writeMachineData.getNid());
		
		WriteMachineData updateData = select(writeMachineData.getNid());
		
		if (null == updateData) {		
			insert(writeMachineData);
		} else {
			updateData.setCmd(writeMachineData.getCmd());			
			update(updateData);
		}		
		
		/* sleep for 8 secs
		try {
			logger.debug("Wait for 8 seconds!");
			Thread.sleep(8000);
		} catch (InterruptedException e) {
			logger.error(e.getLocalizedMessage());
		}
		
		WriteMachineData zeroData = new WriteMachineData();
		zeroData.setNid(writeMachineData.getNid());
		zeroData.setCmd("0");		
		*/
		
		/* Timer Task
		Timer timer = new Timer();		
		WriteMachineDataTimerTask timerTask = new WriteMachineDataTimerTask();
		timerTask.setData(zeroData);		
		timer.schedule(timerTask, 4000);
		*/
		
		/* sleep for 8 secs
		logger.debug("Write Zero Data: " + zeroData);
		update(zeroData);
		*/
	}

	@Override
	@Transactional
	public int update(WriteMachineData writeMachineData) {
		writeMachineDataDao.update(writeMachineData);
		return 0;
	}
	
	@Transactional
	public void updateZero(WriteMachineData writeMachineData) {
		logger.debug("Update Zero!");
		writeMachineDataDao.update(writeMachineData);
	}
	
	
	private final class WriteMachineDataTimerTask extends TimerTask {

		private WriteMachineData data;
			
		@Override
		public void run() {
			updateZero(this.getData());			
		}

		/**
		 * @return the data
		 */
		private WriteMachineData getData() {
			return data;
		}

		/**
		 * @param data the data to set
		 */
		public void setData(WriteMachineData data) {
			this.data = data;
		}
		
	}

}
